
import useStore from '@/store/index';
import { heyuiConfig, modal } from 'heyui';
import mitt from "mitt";
heyuiConfig.config("dict.keyName", "value");
heyuiConfig.config("dict.titleName", "label");
heyuiConfig.config("autocomplete.default.keyName", "value");
heyuiConfig.config("autocomplete.default.titleName", "label");

export default {
  install: (app) => {
    app.config.globalProperties.$success = 200;
    app.config.globalProperties.$bus = mitt();
    app.config.globalProperties.$dictReader = (key, val) => {
      let title = val;
      let data = useStore().dicts[key]
      if (!!data) {
        data.forEach(item => {
          if (item.value == val) {
            title = item.label;
            return title;
          }
        });
      }
      return title;
    };
    app.config.globalProperties.$dyAlert = (data, back) => {
      if (data.code != app.config.globalProperties.$success) {
        modal({
          closeOnMask: false,
          content: `<div class=""><i class="red-color h-icon-error"></i>&nbsp;&nbsp;` + data.msg + `</div>`,
        });
        return;
      }
      modal({
        closeOnMask: false,
        content: `<div class=""><i class="primary-color h-icon-success"></i>&nbsp;&nbsp;` + data.msg + `</div>`,
        buttons: [{
          type: 'ok',
          name: '确认',
          style: 'primary'
        }],
        events: {
          ok: (modal) => {
            modal.close();
            if (back != false) {
              app.config.globalProperties.$router.back();
            } else {
              app.config.globalProperties.$bus.emit("ReloadData");
            }
          },
        }
      });
    };
    app.config.globalProperties.$split = (data) => {
      if (!!data && data.indexOf(",") != -1) {
        data = data.split(",");
        return data;
      }
      return [data];
    };
    app.config.globalProperties.$join = (data) => {
      if (!!data && data instanceof Array && data.length > 0) {
        data = data.join(",");
      }
      return data;
    };
  }
};
